// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var top = require("../top/top_white");
	var footer = require("../footer/footer_gray");
	var wait = 60;
	var registerUuid;
	var mobile;
	var authenticode;
	var flag = true;
	var databind = require("databind");
	var tools = require("tools");
	var init = function() {
		top.init("注册");
		footer.init();
		$("#head").removeClass("none");
		$("#main").removeClass("none");
		$("#footer").removeClass("none");
		initEvent();
		router.init();
	};

	var initEvent = function() {

		$("#main").on("click", "#next a", function() {
			var phoneNum = $("#phone input").val();
			var code = $("#code input").val();

			if (phoneNum == null || phoneNum == "") {
				base.alert(2, "请输入手机号码！");
			} else if (!validatePhoneNum(phoneNum)) {
				base.alert(2, "请输入正确的手机号码！");
			} else if (code == null || code == "") {
				base.alert(2, "请输入验证码！");
			} else if (code.length != 6) {
				base.alert(2, "请输入正确的验证码！");
			} else if (!$("#accept input").prop("checked")) {
				base.alert(2, "请勾选 《指尖金融服务协议》");
			} else {
				if (registerUuid == "" || registerUuid == null) {
					base.alert("该手机号码已被注册！");
				} else {
					databind.submitDom($(".registerContent"), function(data) {
						if (data.status == 200) {
							authenticode = $("#authenticode").val();
							router.route("register/register_setPsw.html?mobile=" + mobile + "&registerUuid=" + registerUuid + "&authenticode=" + authenticode);
						} else {
							base.alert(3, data.message);
						}
					});
				}
			}
		});
		
		tools.geetest($("#popup-submit"), $("#popup-captcha"), function(){
			var phoneNum = $("#phone input").val().trim();
			if (phoneNum == "") {
				base.alert(2, "请输入手机号码！");
				return;
			} else if (!validatePhoneNum(phoneNum)) {
				base.alert(2, "请输入正确的手机号码！");
				return;
			}
			if (flag && wait == 60) {
				flag = false;
				registerEnterpriseAccount();
			}
			if (wait > 0) {
				$("#popup-submit").unbind("click");
			}
		}, function(){
			
		});

	};
	
	//限制60秒内不能重复获取验证码
	var time = function() {
		if (wait == 0) {
			$("#code a").removeAttr("disabled");
			$("#code a").html("获取验证码");
			wait = 60;
		} else {
			$("#code a").unbind("click");
			$("#code a").attr("disabled", "true");
			$("#code a").html("重新发送(" + wait + ")");
			wait--;
			setTimeout(function() {
				time()
			},
			1000);
		}
	}

	//验证手机号码是否正确
	var validatePhoneNum = function(str) {
		var regMobile = /^0?1[3|4|5|7|8][0-9]\d{8}$/ //手机
		if (regMobile.test(str)) {
			return true;
		} else {
			return false;
		}
	}

	//获取验证码
	var registerEnterpriseAccount = function() {
		mobile = $("#phone input").val();
		base.fnAjax({
			action: "registerEnterpriseAccount.json",
			nocode: true,
			oData: {
				mobile: mobile
			}, //传递参数
			fnSuccess: function(data) {
				flag = true;
				if (data.status == 200) {
					base.alert(1, "验证码发送成功！");
					time();
					$(".registerUuid").val(data.datas[0].registerUuid);
					registerUuid = data.datas[0].registerUuid;
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};

	return {
		init: init
	};
});